package com.example.lottery.service.impl;

import com.example.lottery.controller.param.CreatePrizeParam;
import com.example.lottery.controller.param.PageParam;
import com.example.lottery.dao.datobject.PrizeDO;
import com.example.lottery.dao.mapper.PrizeMapper;
import com.example.lottery.service.PictureService;
import com.example.lottery.service.PrizeService;
import com.example.lottery.service.dto.PageListDTO;
import com.example.lottery.service.dto.PrizeDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class PrizeServiceImpl implements PrizeService {
    @Autowired
    private PictureService pictureService;
    @Autowired
    private PrizeMapper prizeMapper;
    @Override
    public Long createPrize(CreatePrizeParam param, MultipartFile picFile) {
        //上传图片
        String fileName = pictureService.savePicture(picFile);

        //存入奖品表里面
        PrizeDO prizeDo=new PrizeDO();
        prizeDo.setName(param.getPrizeName());
        prizeDo.setDescription(param.getDescription());
        prizeDo.setImageUrl(fileName);
        prizeDo.setPrice(param.getPrice());
        prizeMapper.insert(prizeDo);
        return prizeDo.getId();
    }

    @Override
    public PageListDTO<PrizeDTO> findPrizeList(PageParam param) {
        //返回总量
        int total=prizeMapper.count();
        //查询当前页面列表
       List<PrizeDTO> prizeDTOList=new ArrayList<>();
       List<PrizeDO> prizeDoList=prizeMapper.selectPrizeList(param.offset(),param.getPageSize());
       for(PrizeDO prizeDo:prizeDoList){
           PrizeDTO prizeDTO=new PrizeDTO();
           prizeDTO.setPrizeId(prizeDo.getId());
           prizeDTO.setName(prizeDo.getName());
           prizeDTO.setDescription(prizeDo.getDescription());
           prizeDTO.setImageUrl(prizeDo.getImageUrl());
           prizeDTO.setPrice(prizeDo.getPrice());
           prizeDTOList.add(prizeDTO);
       }
       return new PageListDTO<>(total,prizeDTOList);

    }
}
